#-*-coding:utf-8-*-
'''
Created on 2013-3-24
C:/Users/zhaoyh/workspace/sysmanager/checking.py
@author: zhaoyh
'''
import xml.etree.ElementTree as ET
from PyQt4 import QtCore, QtGui
from ui_check import Ui_Check
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
#读取xml文件

def load_xml_file(fileName):
    checkingui=Ui_Check()
    checkjgw = QtGui.QWidget()
    checkingui=Ui_Check()
    checkingui.setupUi(checkjgw)
    checkjgw.setObjectName(_fromUtf8("checkw"))
    root = ET.parse(fileName).getroot()
    module=root.find('module').text
    #获取所有list节点
    all_rule = root.findall('rule')
    

    
    #遍历list节点的子元素
    for rule in all_rule:
        item = QtGui.QTreeWidgetItem()
        name = rule.find('name').text
        type = rule.find('type').text
        warninglevel = rule.find('warninglevel').text
        massage = rule.find('massage').text
        sql = rule.find('sql').text
#        Ui_Check.messagelbl.setText(name)
        checkrsItem=__check(type,sql)
        if checkrsItem:
            item.setText(0,name+warninglevel)#检查项            
            item.setIcon(0,checkingui.fineicon)
            item.setIcon(1,checkingui.warningicon)
            item.addChildren(checkrsItem)
            checkingui.treeWidget.topLevelItem(0).addChild(item)#.addChilds(checkrsItem)
            print item.childCount() 
        else:
            item.setIcon(0,checkingui.fineicon)
            item.setIcon(1,checkingui.warningicon)
            checkingui.checkjgw.treeWidget.topLevelItem(1).addChild(item)
        #返回list 使用dict
        print name,type,warninglevel,massage,sql
def __check(type,sql):
    '''
    retrun Item list
    '''
    itemlist=[]
    sql='1234567890ssss'
    for i in sql:            
        item = QtGui.QTreeWidgetItem()
        item.setText(0,i)
#        item.setIcon(0,checkingui.fineicon)
#        item.setIcon(1,checkingui.warningicon)
        itemlist.append(item)        
    return itemlist
        

#def ruleaddtree(name,warninglevel,massage):
#    item = QtGui.QTreeWidgetItem()
#    item.setText(0, _translate("Check", "账务2", None))
#    checkingui.treeWidget.topLevelItem(0).addChild(item)
#    #返回多行结果怎么处理？
#    item.setIcon(0,checkingui.fineicon)
#    item.setIcon(1,checkingui.warningicon)
#def massageaddtree(name,warninglevel,massage):
#    item = QtGui.QTreeWidgetItem()
#    item.setText(0, _translate("Check", "账务2", None))
#    checkingui.treeWidget.topLevelItem(0).addChild(item)
#    #返回多行结果怎么处理？
#    item.setIcon(0,checkingui.fineicon)
#    item.setIcon(1,checkingui.warningicon)

if __name__ == '__main__':

    load_xml_file('check/gl.xml')